package org.darkforest.common.services.impl;

import org.darkforest.common.services.ProxyManagerService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.dao.mapper.*;
import org.darkforest.enums.MemberType;
import org.darkforest.enums.ProxyApplyStageEnum;
import org.darkforest.model.Member;
import org.darkforest.model.Proxy;
import org.darkforest.model.ProxyGoods;
import org.darkforest.model.StatProxy;
import org.darkforest.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by Libing on 2015/5/23.
 */

@Service
public class ProxyManagerServiceImpl implements ProxyManagerService {

    @Autowired
    private ProxyMapper proxyMapper;

    @Autowired
    private ProxyExtendsMapper proxyExtendsMapper;

    @Autowired
    private ProxyGoodsMapper proxyGoodsMapper;
    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private StatProxyMapper statProxyMapper;



    public ProxyApproveVO ApproveProxyDetail(Integer proxyId) {
        ProxyApproveVO proxyApproveVO = new ProxyApproveVO();
        proxyApproveVO.setProxy(proxyMapper.selectByPrimaryKey(proxyId));
        proxyApproveVO.setProxyExtends(proxyExtendsMapper.findProxyExtendsByProxyId(proxyId));


        proxyApproveVO.setProxyGoodsList(proxyGoodsMapper.findProxyGoodsByCondition(new ProxyGoods(proxyApproveVO.getProxy().getTeamId())));
        return proxyApproveVO;
    }

    @Override
    public Proxy addProxy(Proxy proxy) {
        proxy.setApproveResult(0);
        proxy.setApplyDate(new Date());
        proxy.setApplyStage(ProxyApplyStageEnum.NEW);
        proxyMapper.insert(proxy);
        //查询获取team_id
        proxy.setTeamId(this.getTeamId(proxy.getMemberProxyRel(),proxy.getProxyId()));
        proxyMapper.updateTeamId(proxy);
        return proxy;
    }


    /**
     * 根据会员编号获取
     * @param memberId
     * @return
     */
    private Integer getTeamId(Integer memberId,Integer proxyId){
        Integer res = proxyId;
        MemberProxyVo parent_proxy = this.getNearProxy(memberId);
        if(parent_proxy!=null){
            res = parent_proxy.getTeam_id();
        }
        return res;
    }

    /**
     * 获取距离会员最近的代理商信息
     * @param memberId
     * @return
     */
    private MemberProxyVo getNearProxy(Integer memberId){
        MemberProxyVo proxyVo = null;
        //获取用户信息做为循环初始值
        Member member = memberMapper.findMemberInfoByPrimaryKey(memberId.toString());
        //有推荐人时检查推荐人中是否有代理上，否则直接返回proxy_id做为teamId
        int parent_id = member.getRecommendId();
        Integer team_id = null;
        MemberProxyVo tmp = null;
        while (parent_id!=0 && team_id==null){
            tmp = memberMapper.getMemberProxyInfo(parent_id);
            team_id = tmp.getTeam_id();
            //更新循环变量
            parent_id = tmp.getRecommend_id();

        }
        //循环结束如果team_id则赋值取代本身的proxy_id
        if(team_id!=null){
            proxyVo = tmp;
        }
        return proxyVo;
    }

    /**
     * 更新申请阶段
     * @param proxyId
     * @param stage
     * @return
     */
    @Override
    public int updateStage(Integer proxyId, ProxyApplyStageEnum stage) {
        Proxy proxy = new Proxy();
        proxy.setProxyId(proxyId);
        proxy.setApplyStage(stage);
        proxy.setApproveDate(new Date());
        return proxyMapper.updateStage(proxy);
    }

    @Override
    public Proxy getProxyByMemId(Integer memberId) {
        Proxy proxy = new Proxy();
        proxy.setMemberProxyRel(memberId);
        List<Proxy> proxyList = proxyMapper.findProxyByCondition(proxy);
        if(proxyList!=null && proxyList.size()>0){
            proxy = proxyList.get(0);
        }else{
            proxy = null;
        }
        return proxy;
    }

    @Override
    public StatProxyVo getStatInfo(Integer proxy_id, Integer team_proxy_id,Integer memberId) {
        StatProxyVo statInfo = new StatProxyVo();
        statInfo.setTeamId(team_proxy_id);
        //获取团队名
//        Proxy teamInfo = proxyMapper.selectByPrimaryKey(team_proxy_id);
//        Map<String,Integer> param = new HashMap<String,Integer>();
//        param.put("state",1);
//        param.put("teamId",team_proxy_id);
        //获取所在团队总人数
//        Integer teamCnt = proxyMapper.getProxyCount(param);
//        statInfo.setTeamName(teamInfo.getTeamName()==null?teamInfo.getContactName():teamInfo.getTeamName());
//        statInfo.setTeamCnt(teamCnt);
        //获取该用户的团队总人数
        StatProxy statProxy = statProxyMapper.selectByProxyId(proxy_id);
        statInfo.setTeamCnt(statProxy.getTeamNum());
        statInfo.setTeamName(statProxy.getStatpProxyname());
        //获取直属代理商
        Integer proxyCnt = proxyMapper.getUnderProxyCnt(memberId);
        //获取直属推荐用户
        Integer memberCnt = memberMapper.getUnderMemberCnt(memberId);
        statInfo.setMemberCnt(memberCnt);
        statInfo.setProxyCnt(proxyCnt);
        return statInfo;
    }

    @Override
    public SubProxyInfo getProxyList(SubProxyInfo page) {
        page.setResultList(proxyMapper.getUnderProxyList(page));
        return page;
    }

    @Override
    public SubMemberInfo getSubMemberList(SubMemberInfo memberInfoPage) {
        memberInfoPage.setResultList(proxyMapper.getUnderMemberList(memberInfoPage));
        return memberInfoPage;
    }

    @Override
    @Transactional
    public void aduitProxy(Integer memberId, Proxy proxy) {
        this.proxyMapper.updateByPrimaryKeySelective(proxy);
//        this.updateStage(proxy.getProxyId(),complete);
        //审核通过时，更新会员状态，增加统计数据
        if(proxy.getApproveResult()==1){
            //更新会员状态
            Member member = new Member();
            member.setMemberId(memberId);
            member.setMemberTypeEnum(MemberType.MERCHANT);
            memberMapper.updateMemberType(member);
            StatProxy statProxy = new StatProxy();
            statProxy.setStatpProxyid(proxy.getProxyId());
            statProxy.setStatpUpdatetime(DateUtil.unixTimeStampInt().intValue());
            statProxy.setSaleAmtGoods(new BigDecimal(0.00));
            statProxy.setSaleAmtProxygoods(new BigDecimal(0.00));
            statProxy.setTeamSaleamtGoods(new BigDecimal(0.00));
            statProxy.setTeamSaleamtProxygoods(new BigDecimal(0.00));
            statProxy.setTeamNum(0);
            statProxyMapper.insert(statProxy);
        }
    }
}
